package com.example.neusoft.cook.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.neusoft.cook.R;
import com.example.neusoft.cook.base.BaseFragment;
import com.example.neusoft.cook.bean.SampleUser;
import com.example.neusoft.cook.bean.SampleUserInfo;
import com.example.neusoft.cook.interf.Config;
import com.example.neusoft.cook.interf.RetrofitService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends BaseFragment {
    private EditText editTextUserName;
    private EditText editTextPassword;
    private EditText editTextPhone;
    private EditText editTextAddress;
    private Button buttonConfirm;

    //private SampleUserInfo sampleUserInfo;

    public MyFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        editTextUserName = (EditText) view.findViewById(R.id.textView_username);
        editTextPassword = (EditText) view.findViewById(R.id.textView_password);
        editTextPhone = (EditText) view.findViewById(R.id.textView_phone);
        editTextAddress = (EditText) view.findViewById(R.id.textView_address);

        Retrofit retrofit=new  Retrofit.Builder()
                .baseUrl(Config.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RetrofitService retrofitService=retrofit.create( RetrofitService.class);
        Call<SampleUserInfo>  call=retrofitService.getUserById(getUserId());
        call.enqueue(new Callback<SampleUserInfo>() {
            @Override
            public void onResponse(Call<SampleUserInfo> call, Response<SampleUserInfo> response) {
                editTextUserName.setText(response.body().getUsername());
                editTextPassword.setText(response.body(). getUserpass());
                editTextAddress.setText(response.body().getAddress());
                editTextPhone.setText(response.body().getMobilenum());
            }

            @Override
            public void onFailure(Call<SampleUserInfo> call, Throwable t) {

            }
        });

        buttonConfirm = (Button) view.findViewById(R.id.button_confirm);

        buttonConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Config.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                RetrofitService retrofitService = retrofit.create(RetrofitService.class);

                String username = editTextUserName.getText().toString();
                String password = editTextPassword.getText().toString();
                String phone = editTextPhone.getText().toString();
                String address = editTextAddress.getText().toString();

                Call<SampleUser> call = retrofitService.updateUserById(getUserId(),username, String.valueOf(password), phone, address);
                call.enqueue(new Callback<SampleUser>() {
                    @Override
                    public void onResponse(Call<SampleUser> call, Response<SampleUser> response) {
                        if (response.body().getSuccess().equals("1")){
                            Toast.makeText(getActivity(),"修改成功",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(getActivity(),"修改失败",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<SampleUser> call, Throwable t) {

                    }
                });
            }
        });


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my, container, false);
    }

}
